package com.ruoyi.test.javase;

import com.ruoyi.test.javase.entity.User;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class lambda {
    public static void main(String[] args) {
        List<User> users = new ArrayList<>();
        users.add(new User(18,"张三"));
        users.add(new User(17,"李四"));
        users.add(new User(19,"王五"));
        //打印user
        users.forEach(System.out::println);
//        System.out.println(users);
        users.sort(customComparator);
        users.sort(Comparator.comparing(User::getAge).reversed());

        users.forEach(System.out::println);
    }

    public static Comparator<User> customComparator = (u1, u2) -> {
        int ageComparison = Integer.compare(u1.getAge(), u2.getAge());
        if (ageComparison != 0) {
            return ageComparison;
        }
        // 如果年龄相同，按姓名排序
        return u1.getName().compareTo(u2.getName());
    };
}
